<template>
	<om-formsub-outer :index="index" :checkedit="checkedit">
		<om-autoform ref="autoform" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	const app = getApp();
	import {
		buildSubFormData,
		subOnLoad,
		changeEditor,
		configSelectData,
		hasValue
	} from '../../../static/js/formUtil.js';
	import {
		ignoreHtml,
		errorModal,
		ptFloat,
		minusF,
		confirmModal,
		getDateStr,
		getDayOfWeek
	} from '@/static/js/common.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId','token'])
		},
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildSubFormData({
				userCacheKey: "hr-kq-overtime-user-cache",
				user1CacheKey:"hr-kq-overtime-user1-cache",
				instCacheKey: "hr-kq-overtime-inst-cache",
				businessLeaveCache:"hr-kq-overtime-businessLeave-cache",
				overTimeDay: "",
				editId:"",
				isVerify:true,
				x1:'',
				x2:'',
				data:{},
				instId:"",
			})
		},
		methods: {
			async verifySubConfirm(){
				let _that = this
				return Promise.all([
					_that.om.request(
						'toverTime/verifyWitness',
						{
							leaveFlag:_that.$refs.autoform.formdata.businessLeave?'true':'',
							witnessId:_that.$refs.autoform.formdata.witnessId
						}
					)
				]).then(res=>{
					if(res[0].value && !res[0].value==''){
						return res[0].value
					}else{
						return true
					}
				})
			},
			verifyWitness(){
				let _that = this
				if(_that.$refs.autoform.formdata.userId){
					_that.$refs.autoform.changeEditor('overTimeInfo', 'witnessId', {
						link: "../../public/pageSearch/pageSearch",
						"linkParam": {
							other: JSON.stringify({
								id:'', 
								noUserIdStr:_that.$refs.autoform.formdata.userId
							}),
							valuefield: "id",
							searchfield: 'q',
							initload: true,
							namefield: "userPosName",
							url: "orgInstPosUserOfficial/findListByOrgId1",
							cachename: _that.user1CacheKey,
							pagination: true,
							placeholder: "请输入姓名"
						}
					}, null, _that.autoformdata)
					return true
				}else{
					let title  = _that.$refs.autoform.getEditorTitle("overTimeInfo","userId")
					_that.om.toast("请先选择"+title)
					return false
				}
			},
			isInitUser(){
				let _that = this
				_that.om.request(
					"dataDict/getIntByCode",
					{
						code:'DD016169038'
					}
				).then(res=>{
					if(res.value){
						if(!_that.$refs.autoform.formdata.userId && res.value >0){
							_that.$refs.autoform.formdata.userName = _that.userName
							_that.$refs.autoform.formdata.userId = _that.userId
							_that.getClassesListByUserId(_that.$refs.autoform.formdata.userId)
							_that.getDataInfo(_that.userId)
						}
						if(_that.$refs.autoform.formdata.userId && res.value >0){
							_that.getClassesListByUserId(_that.$refs.autoform.formdata.userId)
							_that.getDataInfo(_that.$refs.autoform.formdata.userId)
						}
					}
				})
			},
			getClassesListByUserId(id){
				let _that = this
				_that.om.request(
					"ttimeTable/getClassesListByUserId",
					{
						userId: id,
						classesId: ''
					}
				).then(res=>{
					_that.$refs.autoform.changeEditor("overTimeInfo","classesId","data",configSelectData(res,'classesId',"classesName"))
					_that.$refs.autoform.initSelectIndex("overTimeInfo","classesId")
				})
			},
			verifyLink(){
				let _that = this 
				_that.om.request(
					"businessLeaveList/getListByUserIdDate",
					{
						userId: _that.$refs.autoform.formdata.userId,
						date:_that.overTimeDay
					}
				).then(res=>{
					if(res.value.length>1){
						uni.setStorage({
							key:_that.businessLeaveCache,
							data:res.value
						})
						uni.navigateTo({
							url:'/pages/form/hr-kq-overtime/businessleave'
						})
					}else{
						_that.om.getFormInfoByFileCode(res.value[0].code, res.value[0].busCode)
					}
				})
			},
			onSelectOverTimeDate(value){
				let _that = this
				_that.$refs.autoform.formdata.week = getDayOfWeek(value)
				_that.om.request(
					"hrPosUtil/verifyFormDate", {
						date: value,
						busCode: _that.busCode
					},
					"post"
				).then(res => {
					if (res.value) {
						errorModal(_that, '提示信息', ignoreHtml(res.value), () => {
							_that.$refs.autoform.clear("overTimeInfo", "overTimeDate")
							_that.$refs.autoform.clear("overTimeInfo", "week")
						})
					}else{
						_that.isInitUser()
					}
				})
			},
			getDataInfo(userId){
				let _that = this
				_that.om.request(
					"toverTime/getDataInfo",
					{
						userId:userId,
						date:_that.$refs.autoform.formdata.overTimeDate
					}
				).then(res=>{
					delete res.value.userId
					delete res.value.userName
					delete res.value.instructions
					Object.assign(_that.$refs.autoform.formdata,res.value)
					if(_that.$refs.autoform.hasEditor("overTimeInfo","businessLeave") && _that.$refs.autoform.formdata.businessLeave){
						_that.$refs.autoform.formdata.isoutNoAttence = true
						_that.$refs.autoform.formdata.isoutNoAttenceStr = true
						_that.$refs.autoform.changeEditor("overTimeInfo","businessLeave","showArrow",true)
						//1.verifyLink 用于判断是否含有跳转验证的属性
						//2.verifyLink 方法名
						_that.$refs.autoform.changeEditor("overTimeInfo","businessLeave","verifyLink","verifyLink")
					}else{
						_that.$refs.autoform.formdata.isoutNoAttence = false
						_that.$refs.autoform.formdata.isoutNoAttenceStr = false
						_that.$refs.autoform.changeEditor("overTimeInfo","businessLeave","showArrow",false)
						_that.$refs.autoform.changeEditor("overTimeInfo","businessLeave","verifyLink","")
					}
					_that.userConfirm(res.value.salaryPosId)
					_that.changeCostInstType()
				})
			},
			verifyTime(st,et){
				let _that = this
				_that.om.request(
					"toverTime/verifyTime",
					{
						date: _that.overTimeDay,
						userId: _that.$refs.autoform.formdata.userId,
						startTime: st,
						endTime: et,
						editId: _that.editId
					}
				).then(res=>{
					if(res.value){
						_that.isVerify = true
						errorModal(_that,"提示信息",ignoreHtml(res.value),()=>{
							return
						})
					}else{
						_that.isVerify = false
					}
				})
			},
			userConfirm1(salaryPosId) {
				let _that = this
				if (!_that) {
					return
				}
				_that.om.request("tposition/getPosInfo", {
					"posId": salaryPosId
				}).then(res => {
					if(res.value.overTimeTypeForce){
						_that.$refs.autoform.changeEditor("overTimeInfo","salaryHandleId","type","labelbox")
						_that.changeCostInstType()
					}else{
						_that.$refs.autoform.changeEditor("overTimeInfo","salaryHandleId","type","selectbox")
					}
					_that.$refs.autoform.initSelectIndex('overTimeInfo','salaryHandleId')
				})
			},
			userConfirm(salaryPosId) {
				let _that = this
				if (!_that.$refs.autoform) {
					return
				}
				_that.om.request("tposition/getPosInfo", {
					"posId": salaryPosId
				}).then(res => {
					_that.$refs.autoform.formdata.salaryPosId = salaryPosId
					_that.$refs.autoform.formdata.overTimeTypeId = res.value.overTimeTypeId
					_that.$refs.autoform.formdata.overTimeTypeName = res.value.overTimeTypeName
					_that.$refs.autoform.formdata.salaryHandleId = res.value.overTimeTypeId
					_that.$refs.autoform.formdata.salaryHandleName = res.value.overTimeTypeName
					_that.$refs.autoform.formdata.ratio = res.value.overTimeRatio
					if(res.value.overTimeTypeForce){
						_that.$refs.autoform.changeEditor("overTimeInfo","salaryHandleId","type","labelbox")
						_that.changeCostInstType()
					}else{
						_that.$refs.autoform.changeEditor("overTimeInfo","salaryHandleId","type","selectbox")
					}
					_that.$refs.autoform.initSelectIndex('overTimeInfo','salaryHandleId')
				})
			},
			endTimeFunc(e) {
				let _that = this.$refs.autoform
				let endTime = this.timeFunc(e);
				_that.formdata.endTime = endTime;
				_that.clear('overTimeInfo', 'classesId')
				if (_that.formdata.startTime) {
					if (_that.formdata.startTime > _that.formdata.endTime && _that.formdata.endTime != '00:00') {
						_that.formdata.startTime = '';
						_that.formdata.workHoursCode = '';
						_that.formdata.workHoursName = '';
					} else {
						//计算加班工时
						this.workHoursFunc();
						//验证时间是否重叠
						this.verifyTimeFunc(_that.formdata.startTime, _that.formdata.endTime);
						// this.verifyTime(_that.formdata.startTime, _that.formdata.endTime)
					}
				}
			},
			changeCostInstType(){
				let _that = this
				let editor = _that.$refs.autoform.getEditor('overTimeInfo','costIns tId')
				if(editor && editor.type != 'linkbox'){
					_that.$refs.autoform.changeEditor('overTimeInfo','costInstId',"type","linkbox")
					_that.$refs.autoform.changeEditor('overTimeInfo','costInstId',{
						link: "../../public/search/search",
						"linkParam": {
							other: JSON.stringify({
								firstUserId: _that.userId
							}),
							searchfield: 'instName',
							initload: true,
							method: "post",
							namefield: "instname",
							url: "tinstitution/findNotBoard1",
							cachename: _that.instCacheKey,
							placeholder: "请输入机构"
						}
					})
				}
			},
			getSalaryPos() {
				let _that = this
				_that.om.request(
					"tposition/getSalaryPos", 
					{
						userId: _that.$refs.autoform.formdata.userId
					}
				).then(res => {
					_that.$refs.autoform.formdata.costInstId = res.value.instId
					_that.$refs.autoform.formdata.costInstName = res.value.instName
					_that.changeCostInstType()
				})
			},
			onSelectSalaryHandle(item) {
				let _that = this
				_that.$refs.autoform.formdata.ratio = item.ratio
				_that.getSalaryPos()
			},
			classesFunc(e) {
				let _that = this.$refs.autoform;
				_that.formdata.classesId = e.classesId
				_that.formdata.classesName = e.classesName
				_that.om
					.request('ttimeTable/getTime', {
						classesId: _that.formdata.classesId
					})
					.then(res => {
						if (res.succeed) {
							_that.formdata.startTime = res.value.startTime;
							_that.formdata.endTime = res.value.endTime;
							//计算加班工时
							this.workHoursFunc(res.value.ratedTime);
							this.verifyTimeFunc(res.value.startTime, res.value.endTime);
						}
					});
				//验证时间是否重叠
			},
			timeFunc(q) {
				if (this.x1 > 0) {
					let qs = q.split(':');
					if (qs[1].length > 0) {
						qs[1] = Math.round(qs[1] / 10) * 10;
						if (qs[1] == 60) {
							qs[1] = '00';
							if((qs[0] * 1 +1)<10){
								qs[0] = '0'+(qs[0] * 1 + 1);
							}else{
								qs[0] = qs[0] * 1 + 1
							}
							if (qs[0] == 24) {
								qs[0] = '00';
							}
						}else if(qs[1] == 0){
							qs[1] = '00';
						}
					}
					return qs[0] + ':' + qs[1];
				} else {
					return q;
				}
			},
			//验证时间是否重叠
			verifyTimeFunc(sT, eT) {
				let _that = this;
				if (!sT || !eT ) {
					return;
				}
				_that.om
					.request('toverTime/verifyConflictTime', {
						startTime: sT,
						endTime: eT == '00:00' ? '24:00' : eT,
						date: _that.$refs.autoform.formdata.overTimeDate,
						userId: _that.$refs.autoform.formdata.userId
					})
					.then(res => {
						if (res.value) {
							errorModal(
								_that,
								'提示信息',
								ignoreHtml(res.value),
								function() {
									_that.$refs.autoform.clear('overTimeInfo', 'classesId')
									_that.$refs.autoform.formdata.startTime = '';
									_that.$refs.autoform.formdata.endTime = '';
									_that.$refs.autoform.formdata.workHoursCode = '';
									_that.$refs.autoform.formdata.workHoursName = '';
								}
							)
						} else {
							if(!_that.$refs.autoform.formdata.classesId){
								this.verifyBreakTime(sT,eT);
							}
						}
					})
			},
			verifyBreakTime(sT,eT) {
				let _that = this
				_that.om.request(
					"toverTime/verifyBreakTime",
					{
						startTime: sT,
						endTime: eT == '00:00' ? '24:00' : eT,
						overTimeDate: _that.overTimeDay,
						userId: _that.$refs.autoform.formdata.userId,
					}
				).then(res=>{
					if(!res.value){
						if (_that.x2) {
							let whc = _that.$refs.autoform.formdata.workHoursCode.replace(':', '.');
							if ((whc > _that.x2) && !_that.$refs.autoform.formdata.classesId) {
								confirmModal(
									_that,
									'提示信息',
									'加班没有选择班次，单条加班时长超过了' + _that.x2 + '小时，加班时段可能包含休息时段，是否继续？',
									function() {},
									function() {
										_that.$refs.autoform.formdata.endTime = '';
										_that.$refs.autoform.formdata.workHoursCode = '';
										_that.$refs.autoform.formdata.workHoursName = '';
									}
								);
							}
						}
					}else{
						errorModal(_that,'提示信息',ignoreHtml(res.value),()=>{
							_that.$refs.autoform.formdata.startTime = '';
							_that.$refs.autoform.formdata.endTime = '';
							_that.$refs.autoform.formdata.workHoursCode = '';
							_that.$refs.autoform.formdata.workHoursName = '';
						})
					}
				})
			},
			workHoursFunc(q) {
				let _that = this.$refs.autoform;
				if (!q) {
					let st = _that.formdata.startTime.replace(':', '.');
					let et = _that.formdata.endTime.replace(':', '.');
					if (_that.formdata.endTime == '00:00') {
						et = '24.00';
					}
					if (et.substring(3, 5) < st.substring(3, 5)) {
						et -= 0.4;
					}
					q = (et - st).toFixed(2) + '';
				} else {
					let q1 = q.split('.');
					q = q1[0] + '.' + parseInt(q1[1]) * 6;
				}
				q = q.replace('.', ':');
				let qs = q.split(':');
				if (qs[0] && qs[0].length == 1) {
					qs[0] = '0' + qs[0];
				}
				if (qs[1] && qs[1].length == 1) {
					qs[1] = qs[1] + '0';
				}
				q = qs[0] + ':' + qs[1];
				let workHoursName = parseInt(qs[0]) + '小时' + parseInt(qs[1]) + '分钟';
				if (qs[0] == '00') {
					workHoursName = parseInt(qs[1]) + '分钟';
				}
				if (qs[1] == '00') {
					workHoursName = parseInt(qs[0]) + '小时';
				}
				if (qs[0] == '00' && qs[1] == '00') {
					workHoursName = '0';
				}
				_that.formdata.workHoursCode = q;
				_that.formdata.workHoursName = workHoursName;
			},
			startTimeFunc(e) {
				let _that = this.$refs.autoform
				let startTime = this.timeFunc(e);
				_that.formdata.startTime = startTime;
				_that.clear('overTimeInfo', 'classesId')
				_that.clear('overTimeInfo', 'endTime')
				if (_that.formdata.endTime) {
					if (_that.formdata.startTime > _that.formdata.endTime && _that.formdata.endTime != '00:00') {
						_that.formdata.endTime = '';
						_that.formdata.workHoursCode = '';
						_that.formdata.workHoursName = '';
					} else {
						//计算加班工时
						this.workHoursFunc();
						//验证时间是否重叠
						this.verifyTimeFunc(_that.formdata.startTime, _that.formdata.endTime);
					}
				}
			},
			initLoad() {
				let _that = this
				let pro2 = _that.om.request("wxPosBusiAuth/isnewList", {
					code: "HRS1204"
				}, "post")
				let pro3 = _that.om.request("askLeaveDict/getOverTimeList")
				Promise.all([pro2, pro3]).then(res => {
					//计酬下拉
					_that.$refs.autoform.changeEditor('overTimeInfo', 'salaryHandleId', 'data', configSelectData(res[1], 'id',
						'dicname'))
					_that.$refs.autoform.initSelectIndex("overTimeInfo","salaryHandleId")	
				})
			}
		},
		onShow() {
			let _that = this
			if (_that.$refs.autoform) {
				uni.getStorage({
					key: _that.userCacheKey,
					success: (res) => {
						if (res.data) {
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.userId = d.id
							formdata.userName = d.username
							_that.data = formdata
							_that.getDataInfo(d.id)
							_that.getClassesListByUserId(d.id)
							uni.removeStorage({
								key: _that.userCacheKey
							})
						}
					}
				}),
				uni.getStorage({
					key:_that.user1CacheKey,
					success:(res)=>{
						if(res.data){
							let d  = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.witnessId = d.userId
							formdata.witnessName = d.userPosName
							uni.removeStorage({
								key:_that.user1CacheKey
							})
						}
					}
				});
				uni.getStorage({
					key: _that.instCacheKey,
					success: (res) => {
						if (res.data) {
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.costInstId = d.id
							formdata.costInstName = d.instName
							uni.removeStorage({
								key: _that.instCacheKey
							})
						}
					}
				})
			}
		},
		onLoad(e) {
			let _that = this
			subOnLoad(_that, e, function() {
				if (e.configDataCacheKey) {
					_that.editId = e.editId
					_that.instId = e.instId
					uni.getStorage({
						key: e.configDataCacheKey,
						success: function(res) {
							if (res.data && Object.prototype.toString.call(res.data) === '[object Array]') {
								_that.autoformdata = res.data
								Object.assign(_that.initValue, _that.formdata)
								_that.autoformdata[0].order = _that.order
								_that.overTimeDay = e.date?e.date:_that.formdata.overTimeDate
								_that.om.request('wxOverTime/getConst').then(res => {
									if (res.succeed) {
										_that.busCode = res.value.busCode || 'HRS1204';
										_that.x1 = res.value.x1;
										_that.x2 = res.value.x2;
										_that.initLoad();
									}
								});

								changeEditor('overTimeInfo', 'userId', {
									link: "../../public/pageSearch/pageSearch",
									"linkParam": {
										other: JSON.stringify({
											instId: _that.instId
										}),
										valuefield: "id",
										searchfield: 'q',
										initload: true,
										namefield: "username",
										notefield:"sexName",
										extrafield:"subMobile",
										url: "orgInstPosUserOfficial/findUserPagerByInst",
										cachename: _that.userCacheKey,
										pagination: true,
										placeholder: "请输入姓名"
									}
								}, null, _that.autoformdata)
							}
						}
					})
				}
				_that.$nextTick(function(){
					if(_that.$refs.autoform.formdata.userId){
						_that.getClassesListByUserId(_that.$refs.autoform.formdata.userId)
					}
					if(_that.$refs.autoform.formdata.salaryPosId){
						_that.userConfirm1(_that.$refs.autoform.formdata.salaryPosId)
					}
					if(_that.$refs.autoform.formdata.costInstId){
						_that.changeCostInstType()
					}
					if(_that.$refs.autoform.formdata.businessLeave){
						if(_that.$refs.autoform.hasEditor("overTimeInfo","businessLeave")){
							if(_that.$refs.autoform.formdata.isoutNoAttenceStr){
								_that.$refs.autoform.formdata.isoutNoAttence = true
								_that.$refs.autoform.formdata.isoutNoAttenceStr = true	
							}
							_that.$refs.autoform.changeEditor("overTimeInfo","businessLeave","showArrow",true)
							//1.verifyLink 用于判断是否含有跳转验证的属性
							//2.verifyLink 方法名
							_that.$refs.autoform.changeEditor("overTimeInfo","businessLeave","verifyLink","verifyLink")
						}
					}
				})
			})
		}
	}
</script>

<style>
</style>
